import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class 批操作 {
    @Test
    public void 使用Statement() {
        Connection connection = null;

        try {
            connection = 连接.getConnection();

            Statement statement = connection.createStatement();

            long start = System.currentTimeMillis();
            for (int i = 1; i <= 2000; i++) {
                String sql = "insert into goods(name) values('Statement_" + i + "')";
                statement.executeUpdate(sql);
            }
            long end = System.currentTimeMillis();
            System.out.println("花费时间为：" + (end - start));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

    }


    @Test
    public void 使用PreparedStatement() {
        Connection connection = null;

        try {
            connection = 连接.getConnection();

            String sql = "insert into goods(name) values(?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            long start = System.currentTimeMillis();
            for (int i = 1; i <= 20000; i++) {
                preparedStatement.setString(1, "PreparedStatement_" + i);
                preparedStatement.executeUpdate();
            }

            long end = System.currentTimeMillis();
            System.out.println("花费的时间为：" + (end - start));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }


    @Test
    public void 使用addBatch(){
        Connection connection = null;

        try {
            connection = 连接.getConnection();

            String sql = "insert into goods(name) values(?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            long start = System.currentTimeMillis();
            for (int i = 1; i <= 20000; i++) {
                preparedStatement.setString(1, "addBatch_" + i);
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
            preparedStatement.clearBatch();

            long end = System.currentTimeMillis();
            System.out.println("花费的时间为：" + (end - start));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    @Test
    public void 设置Commit(){
        Connection connection = null;

        try {
            connection = 连接.getConnection();
            connection.setAutoCommit(false);

            String sql = "insert into goods(name) values(?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            long start = System.currentTimeMillis();
            for (int i = 1; i <= 20000; i++) {
                preparedStatement.setString(1, "commit_" + i);
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
            preparedStatement.clearBatch();

            connection.commit();

            long end = System.currentTimeMillis();
            System.out.println("花费的时间为：" + (end - start));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
